﻿[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.Constraint.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.Constraint.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Additions
{
    using System.ComponentModel;
    using System.Web.UI;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;

    using Generator;

    /// <summary>
    /// The constraint control is a special type of the <see cref="Addition" /> control.
    /// It is an abstract class that needs to have the IsValid method implemented.
    /// The constraints are checked within the ASP.NET validation process and the final result can be found under Page.IsValid property
    /// </summary>
    [DesignTimeVisible(false)]
    [ParseChildren(true)]
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Controls.Additions.Constraint.js")]
    public abstract class Constraint : Addition
    {
        /// <summary>
        /// Gets a custom error text or default error text, if the custom one is not set.
        /// </summary>        
        public string ErrorMessage
        {
            get
            {
                return !string.IsNullOrEmpty(ErrorText) ? ErrorText : DefaultErrorText;
            }
        }

        /// <summary>
        /// Gets or sets the error text.
        /// </summary>        
        [AttributeProperty(Name = "errorText")]
        [Localizable(true)]
        public virtual string ErrorText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the constraint is enabled.
        /// </summary>
        [Browsable(false)]
        public bool Enabled
        {
            get
            {
                if (Owner != null && Owner.Mode == FormBuilderMode.View && !Owner.Basis.Root.ShouldValidateInViewMode)
                {
                    return false;
                }

                return ViewState.GetValue("Enabled", true);                
            }

            set
            {
                ViewState["Enabled"] = value;
            }
        }

        /// <summary>
        /// Gets or sets a value that indicates whether a server control is rendered as UI on the page.
        /// </summary>
        /// <returns>
        /// true if the control is visible on the page; otherwise false.
        /// </returns>
        public override bool Visible
        {
            get
            {
                if (!Enabled)
                {
                    return false;
                }

                return base.Visible;
            }

            set
            {
                base.Visible = value;
            }
        }

        /// <summary>
        /// Gets the default error text.
        /// </summary>
        protected virtual string DefaultErrorText
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Gets information whether the constraint was met.
        /// </summary>
        /// <param name="item">
        /// The item on which the constraint is checked.
        /// </param>
        /// <returns>
        /// was the constraint met
        /// </returns>
        public abstract bool IsValid(OffspringItem item);        
    }
}